<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import Left from '@/components/chat/icons/Left.vue'
import Right from '@/components/chat/icons/Right.vue'
import Home from '@/components/chat/icons/Home.vue'
import AI from '@/components/chat/icons/AI.vue'
import AIChat from '@/components/chat/icons/AIChat.vue'

const isCollapsed = ref(false)
const screenWidth = ref(200)

onMounted(() => {
  getScreenWidth()
  window.onresize = () => {
    return (() => {
      getScreenWidth()
    })()
  }
})
const showCollapse = ref(true)
watch(
  () => screenWidth.value,
  value => {
    if (value <= 768) {
      showCollapse.value = false
      isCollapsed.value = true
    } else {
      showCollapse.value = true
      isCollapsed.value = false
    }
  }
)
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}
const getScreenWidth = () => {
  screenWidth.value =
    window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth
}
</script>

<template>
  <div class="sidebar" :class="{ collapsed: isCollapsed, 'show-collapse': showCollapse }">
    <div class="sidebar-header">
      <div class="logo-container">
        <i class="logo">
          <AI />
        </i>
        <span v-if="!isCollapsed" class="title">AI Chat</span>
      </div>
      <button class="collapse-btn" @click="toggleCollapse" v-if="showCollapse">
        <i class="fas" v-if="!isCollapsed">
          <Left />
        </i>
        <i class="fas" v-if="isCollapsed">
          <Right />
        </i>
      </button>
    </div>
    <nav class="sidebar-nav">
      <router-link to="/home" class="nav-item" active-class="active">
        <span class="nav-icon">
          <i>
            <Home />
          </i>
        </span>
        <span class="nav-text" v-if="!isCollapsed">首页</span>
      </router-link>

      <router-link to="/chat?name=ollama" class="nav-item" active-class="active">
        <span class="nav-icon">
          <i>
            <AIChat />
          </i>
        </span>
        <span class="nav-text" v-if="!isCollapsed">Ollama</span>
      </router-link>

      <router-link to="/chat?name=mcp" class="nav-item" active-class="active">
        <span class="nav-icon">
          <i>
            <AIChat />
          </i>
        </span>
        <span class="nav-text" v-if="!isCollapsed">mcp</span>
      </router-link>
    </nav>
  </div>
</template>

<style scoped>
.sidebar {
  height: 100vh;
  background: #fff;
  color: #2c3e50;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  width: 220px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.05);
  z-index: 1000;
}

.sidebar.collapsed.show-collapse {
  width: 80px;
}

.sidebar.collapsed {
  width: 70px;
}

.sidebar-header {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  width: 32px;
  height: 32px;
  fill: #222;
}

.title {
  width: 100px;
  font-size: 1.25em;
  font-weight: 600;
  letter-spacing: 0.5px;
  margin-left: 15px;
}

.collapse-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 5px;
  border-radius: 8px;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 4px;
}

.collapse-btn:hover {
  background: rgba(0, 0, 0, 0.05);
}

.fas {
  fill: #222;
}

i {
  width: 20px;
  height: 20px;
}

i svg {
  width: 100%;
  height: 100%;
}

.sidebar-nav {
  padding: 16px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  color: #222;
  text-decoration: none;
  transition: all 0.2s ease;
  margin: 4px 0;
  position: relative;
  fill: #222;
}

.nav-item:hover {
  color: var(--chat-primary-color-hover);
  background: var(--chat-background-color);
}

.nav-item.active {
  color: var(--chat-primary-color);
  background: var(--chat-background-color);
}

.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 24px;
  background: var(--chat-primary-color);
  border-radius: 0 4px 4px 0;
}

.nav-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: all 0.5s ease;
}

.nav-item:hover .nav-icon {
  background: var(--chat-primary-border-color);
  fill: var(--chat-primary-color-hover);
}

.nav-item.active .nav-icon {
  background: var(--chat-primary-border-color);
  fill: var(--chat-primary-color);
}

.nav-text {
  margin-left: 12px;
  font-size: 0.95em;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
}

@media (max-width: 768px) {
  .sidebar.collapsed {
    width: 60px;
  }

  .nav-item.active .nav-icon {
    background: transparent;
  }
}

@media (prefers-color-scheme: dark) {
  .sidebar {
    background: #444;
    color: #fff;
  }

  .nav-item {
    fill: #fff;
    color: white;
  }

  .nav-item.active {
    color: var(--chat-primary-border-color);
  }

  .nav-item:hover {
    color: var(--chat-primary-border-color);
  }

  .logo {
    fill: #fff;
  }

  .fas {
    fill: #fff;
  }
}
</style>
